package com.qingke.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Session2
 */
@WebServlet("/Session2")
public class Session2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Session2() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.getWriter().append("��תˮ��Served at: ").append(request.getContextPath());
		HttpSession hs=request.getSession(false);
		if(hs==null){
			hs=request.getSession(true);
			response.getWriter().append("set attribute to session.").append("\n");
			hs.setAttribute("key1", "hello session");
		}
		else{
			response.getWriter().append("session is exist.").append("\n");
			String value=hs.getAttribute("key1").toString();
			response.getWriter().append("get the value from:"+value).append("\n");
		}
		response.getWriter().append(hs.getId());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
